package com.pxmeta.party.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxmeta.common.constant.Status;
import com.pxmeta.common.exception.ServiceException;
import com.pxmeta.party.entity.*;
import com.pxmeta.party.entity.vo.ActivityFoodRecordVO;
import com.pxmeta.party.entity.vo.ActivityFoodSuccessVO;
import com.pxmeta.party.entity.vo.ActivityFoodVO;
import com.pxmeta.party.mapper.ActivityFoodStallMapper;
import com.pxmeta.party.mapper.GiftMapper;
import com.pxmeta.party.mapper.LotteryOpenMapper;
import com.pxmeta.party.service.IActivityFoodStallService;
import com.pxmeta.party.service.IActivityFoodStallUserRecordService;
import com.pxmeta.party.service.ILotteryLuckyUserService;
import com.pxmeta.party.service.ILotteryOpenService;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Random;

@Service
@RequiredArgsConstructor
public class ActivityFoodStallServiceImpl extends ServiceImpl<ActivityFoodStallMapper, ActivityFoodStall> implements IActivityFoodStallService {

    private final IActivityFoodStallUserRecordService activityFoodStallUserRecordService;
    private final UserMapper userMapper;

    private final GiftMapper giftMapper;

    private final ILotteryLuckyUserService lotteryLuckyUserService;


    @Override
    public List<ActivityFoodVO> getFoodStallList(Long userId) {
        return baseMapper.getFoodStallList(userId);
    }

    @Override
    public Integer inputSeasoning(Long userId, Integer count, Integer id) {
        ActivityFoodStall activityFoodStall = this.getById(id);
        if (Status.SUCCESS.getValue().equals(activityFoodStall.getStatus())){
            throw new ServiceException("该礼物已被制作完成！");
        }
        User user = userMapper.selectById(userId);

        Integer foodStallBalance = user.getFoodStallBalance();
        if (foodStallBalance<count){
            throw new ServiceException("调料数量不足！");
        }
        //扣减调料
        foodStallBalance-=count;
        user.setFoodStallBalance(foodStallBalance);
        userMapper.updateById(user);

        Integer currentNumber = activityFoodStall.getCurrentNumber();
        currentNumber += count;
        activityFoodStall.setCurrentNumber(currentNumber);
        Integer totalNumber = activityFoodStall.getTotalNumber();
        Integer status = Status.FAIL.getValue();
        double probability = BigDecimal.valueOf((double) currentNumber / totalNumber).setScale(2, RoundingMode.HALF_UP).doubleValue();
        Random random = new Random();
        int randomNumber = random.nextInt(10); // 生成0到9之间的随机数
        if (randomNumber < probability * 5) {
            status = Status.SUCCESS.getValue();
        }

        if (totalNumber<=currentNumber){
            status = Status.SUCCESS.getValue();
        }

        //保存记录
        ActivityFoodStallUserRecord activityFoodStallUserRecord = new ActivityFoodStallUserRecord();
        activityFoodStallUserRecord.setActivityId(id);
        activityFoodStallUserRecord.setStatus(status);
        activityFoodStallUserRecord.setUserId(userId);
        activityFoodStallUserRecord.setCount(count);
        activityFoodStallUserRecordService.save(activityFoodStallUserRecord);

        //修改状态
        if (status.equals(Status.SUCCESS.getValue())){
            activityFoodStall.setStatus(status);

            LotteryLuckyUser lotteryLuckyUser = new LotteryLuckyUser();
            lotteryLuckyUser.setUserId(userId);
            String nickname = user.getNickname();

            Integer giftId = activityFoodStall.getGiftId();
            Gift gift = giftMapper.selectById(giftId);

            lotteryLuckyUser.setRecordId(id.longValue());
            lotteryLuckyUser.setGiftId(null);
            lotteryLuckyUser.setCount(count);
            lotteryLuckyUser.setPrice(gift.getPrice());
            lotteryLuckyUser.setNickname(user.getNickname());
            lotteryLuckyUser.setAvatar(user.getAvatar());
            lotteryLuckyUser.setContent("恭喜 "+ nickname + " "+ "在吃货大排档中获得"+gift.getName());
            lotteryLuckyUser.setType(6);
            lotteryLuckyUserService.save(lotteryLuckyUser);

            //重置奖励
            ActivityFoodStall newFoodStall = new ActivityFoodStall();
            newFoodStall.setStatus(0);
            newFoodStall.setCurrentNumber(0);
            newFoodStall.setNumber(activityFoodStall.getNumber()+1);
            newFoodStall.setGiftId(activityFoodStall.getGiftId());
            newFoodStall.setTotalNumber(activityFoodStall.getTotalNumber());
            this.save(newFoodStall);
        }
        this.updateById(activityFoodStall);
        return status;
    }

    @Override
    public List<ActivityFoodSuccessVO> getSuccessList(Integer page, Integer size, Integer number) {
        return activityFoodStallUserRecordService.getSuccessList(page,size,number);
    }

    @Override
    public List<ActivityFoodRecordVO> getMyFoodStallRecords(Integer page, Integer size, Long userId, Integer status) {
        return baseMapper.getMyFoodStallRecords(new Page(page,size),userId, status);
    }
}
